/*
 * @Descripttion: 空间查询JS代码
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-22 10:08:14
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-01-05 09:54:42
 */
const app = getApp()
import {
    showMsg
} from '../../../../utils/config.js'
import {
    reqSpaceList,
} from '../../../../request/api'
module.exports = Behavior({
    data: {
        // 空间切换弹窗开始
        showSpace: false, //空间切换弹窗
        activeSpace: 'build',
        buildList: [],
        buildPage: 1,
        buildIsNextPage: true,
        buildId: '',
        floorList: [],
        floorPage: 1,
        floorIsNextPage: true,
        floorId: '',
        roomList: [],
        roomPage: 1,
        roomIsNextPage: true,
        roomId: '', //选中的安装点id
        spaceId: '', //列表筛选结果id
        spaceName: '全部',
        // 空间切换弹窗结束
    },
    methods: {
        // 点击安装点
        clickShowSpace() {
            this.setData({
                showSpace: true
            })
        },
        /**
         * @name: 获取建筑物列表
         * @msg: *parkId：园区id  *pid：父id  name：空间名称
         * @return {*}
         */
        async getBuildList() {
            let params = {
                orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
                pid: '0',
                name: '',
                userId:app.globalData.userId,
                roleName:app.globalData.rolename,
            }
            const res = await reqSpaceList(this.data.buildPage, 20, params)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                rows
            } = res.data
            if (this.data.buildPage === 1) {
                if (rows.length > 0) {
                    rows.unshift({
                        id: '',
                        name: '全部',
                        active: true,
                    })
                }
                this.setData({
                    buildList: rows,
                    buildIsNextPage: true
                })
            } else if (this.data.buildPage > 1 && rows.length) {
                this.setData({
                    buildList: this.data.buildList.concat(rows),
                    buildIsNextPage: true
                })
            } else if (this.data.buildPage > 1 && !rows.length) {
                this.setData({
                    buildIsNextPage: false
                })
            }
        },
        // 滚动到底部触发
        buildLower() {
            if (!this.data.buildIsNextPage) return
            this.setData({
                buildPage: this.data.buildPage + 1,
            })
            this.getBuildList()
        },
        // 建筑物回到最初状态
        getInitBuildList() {
            this.setData({
                buildPage: 1,
                buildIsNextPage: true,
                buildList: []
            })
            this.getBuildList()
        },
        // 点击建筑物
        clickBuild(e) {
            let {
                id
            } = e.currentTarget.dataset

            let {
                buildList,
                floorList,
                roomList
            } = this.data
            if (!id) {
                // 点击全部
                buildList.forEach(item => {
                    if (item.id == id) {
                        item.active = true
                        // this.setData({
                        //     spaceName: item.name
                        // })
                    } else {
                        item.active = false
                    }
                })
                floorList.forEach(item => {
                    item.active = false
                })
                roomList.forEach(item => {
                    item.active = false
                })

                this.setData({
                    buildList,
                    floorList: [],
                    roomList: [],
                    activeSpace: 'build',
                    buildId: id,
                    floorId: '',
                    roomId: '',
                })
            } else {
                // 点击建筑物
                buildList.forEach(item => {
                    if (item.id === id) {
                        item.active = true
                        this.getInitFloorList(item.id)
                        // this.setData({
                        //     spaceName: item.name
                        // })
                    } else {
                        item.active = false
                    }
                })
                floorList.forEach(item => {
                    item.active = false
                })
                roomList.forEach(item => {
                    item.active = false
                })
                this.setData({
                    buildList,
                    floorList,
                    roomList: [],
                    activeSpace: 'build',
                    buildId: id,
                    floorId: '',
                    roomId: '',
                })
            }

        },
        /**
         * @name: 获取楼层列表
         * @msg: *parkId：园区id  *pid：父id  name：空间名称
         * @return {*}
         */
        async getFloorList(pid) {
            let params = {
                orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
                pid,
                name: '',
                userId:app.globalData.userId,
                roleName:app.globalData.rolename,
            }
            const res = await reqSpaceList(this.data.floorPage, 20, params)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                rows
            } = res.data
            if (this.data.floorPage === 1) {
                this.setData({
                    floorList: rows,
                    floorIsNextPage: true
                })
            } else if (this.data.floorPage > 1 && rows.length) {
                this.setData({
                    floorList: this.data.floorList.concat(rows),
                    floorIsNextPage: true
                })
            } else if (this.data.floorPage > 1 && !rows.length) {
                this.setData({
                    floorIsNextPage: false
                })
            }
        },
        // 滚动到底部触发
        floorLower() {
            if (!this.data.floorIsNextPage) return
            this.setData({
                floorPage: this.data.floorPage + 1,
            })
            this.getFloorList(this.data.buildId)
        },
        // 楼层回到初始状态
        getInitFloorList(id) {
            this.setData({
                floorPage: 1,
                floorIsNextPage: true,
                floorList: []
            })
            this.getFloorList(id)
        },
        // 点击楼层
        clickFloor(e) {
            let {
                id
            } = e.currentTarget.dataset
            let {
                floorList,
                roomList,
            } = this.data
            floorList.forEach(item => {
                if (item.id === id) {
                    item.active = true
                    this.getInitRoomList(item.id)
                } else {
                    item.active = false
                }
            })
            roomList.forEach(item => {
                item.active = false
            })
            this.setData({
                floorList,
                roomList,
                activeSpace: 'floor',
                floorId: id,
                roomId: '',
            })

        },
        /**
         * @name: 获取房间列表
         * @msg: *parkId：园区id  *pid：父id  name：空间名称
         * @return {*}
         */
        async getRoomList(pid) {
            let params = {
                orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
                pid,
                name: '',
                userId:app.globalData.userId,
                roleName:app.globalData.rolename,
            }
            const res = await reqSpaceList(this.data.roomPage, 20, params)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                rows
            } = res.data
            if (this.data.roomPage === 1) {
                this.setData({
                    roomList: rows,
                    roomIsNextPage: true
                })
            } else if (this.data.roomPage > 1 && rows.length) {
                this.setData({
                    roomList: this.data.roomList.concat(rows),
                    roomIsNextPage: true
                })
            } else if (this.data.roomPage > 1 && !rows.length) {
                this.setData({
                    roomIsNextPage: false
                })
            }
        },
        // 滚动到底部触发
        roomLower() {
            if (!this.data.roomIsNextPage) return
            this.setData({
                roomPage: this.data.roomPage + 1,
            })
            this.getRoomList(this.data.floorId)
        },
        // 房间回到初始状态
        getInitRoomList(id) {
            this.setData({
                roomPage: 1,
                roomIsNextPage: true,
                roomList: []
            })
            this.getRoomList(id)
        },
        // 点击房间
        clickRoom(e) {
            let {
                id
            } = e.currentTarget.dataset
            let {
                roomList
            } = this.data
            roomList.forEach(item => {
                if (item.id === id) {
                    item.active = true
                } else {
                    item.active = false
                }
            })
            this.setData({
                roomList,
                activeSpace: 'room',
                roomId: id
            })
        },
        // 点击关闭空间切换弹窗
        closeSpace() {
            this.setData({
                showSpace: false
            })
        },
        // 点击确认选择的空间
        confirmSpace() {
            let {
                activeSpace,
                buildId,
                floorId,
                roomId,
            } = this.data
            let spaceId = activeSpace === 'room' ? roomId : activeSpace === 'floor' ? floorId : buildId
            this.setData({
                showSpace: false,
                spaceId,
            })
            this.getInitDataList()
            this.getSpaceName()
        },
        // 获取名称
        getSpaceName() {
            let {
                activeSpace,
                buildList,
                buildId,
                floorList,
                floorId,
                roomList,
                roomId,
                spaceName
            } = this.data
            buildList.forEach(item => {
                if (item.id === buildId) {
                    spaceName = item.name
                }
            })
            floorList.forEach(item => {
                if (item.id === floorId) {
                    spaceName = spaceName + '-' + item.name
                }
            })
            roomList.forEach(item => {
                if (item.id === roomId) {
                    spaceName = spaceName + '-' + item.name
                }
            })
            this.setData({
                spaceName
            })
        }
    }
})